<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Order extends AdminController{
	public $_validation = null;

	function __construct(){
		parent::__construct();
	}

    public function index()
    {
        $this->load->model('MShop');
        $this->load->model('MAmazonStatic');
        $allShops = $this->MShop->getAllShops();

        $this->load->library('FormFilter');
        $this->load->helper('formfilter');
        $page = $this->input->get('page')?$this->input->get('page'):1;
        $config = ['dbname' => 'amazon', 'collection' => 'order_2019_11'];

        $month = filterValue('month');
        $order_number_perpage = filterValue('order_number_perpage');
        $order_number_perpage = ($order_number_perpage?$order_number_perpage:10);
        if ($month){
            $date = str_replace('/','_',$month);
            $config['collection'] = 'order_'.$date;
        }else{
            $last_record = $this->MAmazonStatic->getLast();
            if ($last_record){
                $month = $last_record->month;
                $date = str_replace('/','_',$last_record->month);
                $config['collection'] = 'order_'.$date;
            }
        }

        $this->load->library('MongoZzl',$config, 'mongo_zzl');

        // 条件搜索
        $filter  = [];
        $search = $this->input->post();
        if ($search) {
            $this->session->set_userdata('order_search',$search);
            foreach ($search as $k => $v) {
                if ($k !== 'month' && $k !== 'order_number_perpage' && $v) {
                    $filter[$k] = $v;
                }
            }
        }
        $filter['group_id'] = $this->_user->group_id;

        $order_search = $this->session->userdata('order_search');
        if ($order_search){
            foreach ($order_search as $k => $v){
                if ($k !== 'month' && $k !== 'order_number_perpage' && $v){
                    $filter[$k] = $v;
                }
                $this->assign($k,$v);
            }
        }

        if (isset($order_search['asin']) && $order_search['asin'] !== '') {
            if ($order_search['asin']) {
                $filter['asin'] = array('$exists' => true);
            } else {
                $filter['asin'] = array('$exists' => false);
            }
        }

        $data = $this->mongo_zzl->page($filter,$page, $order_number_perpage);
        $this->pagination($data['count'], '',$order_number_perpage);
        $this->assign('allShops', $allShops);
        $this->assign('list',$data['data']);
        $this->assign('month',$month);
        $this->assign('total_number',$data['count']);
        $this->layout();
    }

    public function python_export(){
        $where = $this->input->post();
        $where['month'] = str_replace('/','_',$where['month']);
        $middle = [$where['month'], $this->_user->group_id, $where['shop_id'], $where['country'], $where['coin_type'], $where['asin']];
        $filter = implode(',',$middle);
        $path = 'python3 export_mongo.py "'.$filter.'" '.' 2>&1';

        exec($path, $out, $res);

        if ($res == 0){
            exit(json_encode([
                'status' => 1,
                'msg' => '导出成功！',
                'data' => $out[count($out) - 1]
            ]));
        }else{
            exit(json_encode([
                'status' => 0,
                'msg' => '导出失败',
                'data' => $out
            ]));
        }
    }

    public function asin_update(){
	    $data = $this->input->post();
        $config = ['dbname' => 'amazon', 'collection' => 'order_2019_11'];
        $date = str_replace('/','_',$data['date']);
        $config['collection'] = 'order_'.$date;
        $this->load->library('MongoZzl',$config, 'mongo_zzl');

        $count = $this->Mongo->update_many(['sku' => $data['sku']],['$set' => ['asin' => $data['asin']]]);
        exit(json_encode([
            'status' => 1,
            'msg'    => '更新数据'.$count.'条'
//            'data'   => $update
        ]));
    }
}
